package in.muton.edushare.pages.publicpages.coursepages;

import in.muton.edushare.common.UserFilter;
import in.muton.edushare.processing.objects.CourseDataHandler;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.InnerList;
import in.muton.panels.paginator.EdushareListView;

import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.Users;

public class CoursePeoplePublic extends CourseBasePublic {
	public CoursePeoplePublic(PageParameters params) {
		super(params);
		if (StringUtil.isValid(params.getString("2"))
				&& params.getString("2").equals("students"))
			student = true;
		else
			student = false;

		Link<Void> l = new Link<Void>("all") {
			private static final long serialVersionUID = -8862024214848066551L;

			@Override
			public void onClick() {
				searchTerm = "";
			}
		};
		l.add(new Label("allCaption", new Model<String>() {
			@Override
			public String getObject() {
				if (student)
					return "All Students";
				return "All Faculty";
			}

			private static final long serialVersionUID = -2699941416497312775L;
		}));
		add(l);

		Form<Void> f = new Form<Void>("searchForm");
		f.add(new TextField<String>("searchTerm", new PropertyModel<String>(
				this, "searchTerm")));
		f.add(new SubmitLink("submitLink"));
		add(f);
		add(new EdushareListView<Users>("userList", new InnerList<Users>() {
			private static final long serialVersionUID = 8488547851111334793L;

			@Override
			public List<IModel<Users>> getObject() {
				if(cInstModel == null){
					return new CayenneList<Users>(null).getObject();
				}
				CayenneList<Users> userList = null;
				if(student)
					userList = new CourseDataHandler(cInstModel).getStudents();
				else{
					userList = new CourseDataHandler(cInstModel).getTeachers();	
					userList.add(new CourseDataHandler(cInstModel).getTA());
				}
				return UserFilter.filterByString(userList, searchTerm).getObject();
			}
		},EdushareListView.SQUARE));

	}

	@Override
	public String getPageName(PageParameters params) {
		if (StringUtil.isValid(params.getString("2"))
				&& params.getString("2").equals("students"))
			return "courseStudentsPublic";
		return "courseFacultyPublic";
	}

	private boolean student = false;
	private String searchTerm = "";
}
